package com.my.talk.dao;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.my.talkshow.entity.TalkShow;

import util.ConnManager;




public class TalkShowDao implements TalkDao {

	@Override
	public boolean addOneTalk(TalkShow talkShow) {
		boolean flag=false;
		Connection conn=null;
		PreparedStatement pst=null;
		try{
		conn=ConnManager.getConnection();
		String sql="insert into up (u_id,s_id,talks) values(?,?,?)";
		pst=conn.prepareStatement(sql);
		pst.setInt(1, talkShow.getuId());
		pst.setInt(2, talkShow.getsId());
		pst.setString(3,  talkShow.getTalks());
		pst.execute();
		pst.close();
		conn.close();
		flag=true;
		}catch(Exception e){
			try {
				pst.close();
			} catch (Exception e2) {
				System.out.println("断开连接出错"+e2.getMessage());
			}
			try {
				conn.close();
			} catch (Exception e3) {
				System.out.println("断开连接出错"+e3.getMessage());
			}
			System.out.println("增加评论出错");
			flag=false;
		}
		return flag;
	}
	

	@Override
	public boolean deletOneTalk(TalkShow talkShow) {
		boolean flag=false;
		Connection conn=null;
		PreparedStatement pst=null;
		try{
			conn=ConnManager.getConnection();
			String sql="delete from talk where u_id=? and s_id=?";
			pst=conn.prepareStatement(sql);
			pst.setInt(1, talkShow.getuId());
			pst.setInt(2,talkShow.getsId());
			pst.execute();
			flag=true;
		}catch(Exception e){
			try {
				pst.close();
			} catch (Exception e2) {
				System.out.println("断开连接出错"+e2.getMessage());
			}
			try {
				conn.close();
			} catch (Exception e3) {
				System.out.println("断开连接出错"+e3.getMessage());
			}
			System.out.println("删除评论出错");
			flag=false;
		}
		return flag;
	}

	@Override
	public List<TalkShow> selectTalks(TalkShow talkShow) {
		List<TalkShow> talkList=new ArrayList<TalkShow>();
		TalkShow talkShow2=null;
		Connection conn=null;
		PreparedStatement pst=null;
		ResultSet rs=null;
		try{
			conn=ConnManager.getConnection();
			String sql="select u_id,u_nick,talks from user,talk where up.u_id=user.u_id and s_id=?";
			pst=conn.prepareStatement(sql);
			pst.setInt(1,talkShow.getsId());
			rs=pst.executeQuery();
			
		while(rs.next()){
			talkShow.setsId(talkShow.getsId());
			talkShow2.setuId(rs.getInt("u_id"));
			talkShow2.setuNick(rs.getString("u_nick"));
			talkShow2.setTalks(rs.getString("talks"));
			talkList.add(talkShow2);
		}
		rs.close();
		pst.close();
		conn.close();
		} catch (Exception e) {
			
			try {
				rs.close();
			} catch (Exception e1) {
				System.out.println("断开连接出错"+e.getMessage());
			}
			try {
				pst.close();
			} catch (Exception e2) {
				System.out.println("断开连接出错"+e2.getMessage());
			}
			try {
				conn.close();
			} catch (Exception e3) {
				System.out.println("断开连接出错"+e3.getMessage());
			}
			System.out.println("查询评论出错"+e.getMessage());
			
		}
		return talkList;
	}
}


